<%@ Page Language="C#" MasterPageFile="~/Site.master" EnableEventValidation="false" AutoEventWireup="true" %>
<%@ Import Namespace="SoftwareMonkeys.Scriptech.Web" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="SoftwareMonkeys.Scriptech.Diagnostics" %>  
<%@ Import Namespace="SoftwareMonkeys.Scriptech.Scripts" %>  
<%@ Register Namespace="SoftwareMonkeys.Scriptech.Web" Assembly="SoftwareMonkeys.Scriptech.Web" TagPrefix="cc" %>
<script runat="server">
	protected string ScriptControlPath = String.Empty;
	protected string ScriptName = String.Empty;
	
	private void Page_Load(object sender, EventArgs e)
	{
		using (LogGroup logGroup = LogGroup.StartDebug("Loading the launch script page."))
		{
			Response.ContentType = "text/HTML";
			
			try
			{
				using (TimeoutExtender extender = TimeoutExtender.NewMinutes(30))
				{
					//Launch();
				}
			}
			catch(Exception ex)
			{
				LogWriter.Error(ex);
				
				throw;
			}
		}
	}
	
	
	private string GetEditLink()
	{
		string path = String.Empty;
	
		using (LogGroup logGroup = LogGroup.StartDebug("Creating the edit link."))
		{
			path = WebUtilities.MapUrlPath("/EditScript.aspx") + "?Script=" + Scripts.GetCurrentScript(true);
			
			LogWriter.Debug("Edit link: " + path);
		}
		
		return path;
	}
	
	private string GetExecuteLink()
	{
		string script = Scripts.GetCurrentScript(true);
		return WebUtilities.ConvertApplicationRelativeUrlToAbsoluteUrl("ExecuteScript.aspx") + "?Script=" + script.Replace("?", "&");
	}
</script>

<asp:Content ContentPlaceHolderID="Body" ID="BodyContent" runat="server">

<% if (Request.QueryString["ShowTemplate"] != "false"){ %>		
		<div style="text-align: right">
			[<a href='<%= GetEditLink() %>'>Edit Script</a>] - 
			[Create: <a href='<%= WebUtilities.MapUrlPath("/EditScript.aspx?Script=newscript.script") %>'>*.script</a>&nbsp;-&nbsp;<a href='<%= WebUtilities.MapUrlPath("/EditScript.aspx?Script=newscript.ascx") %>'>*.ascx</a>] -
			[<a href='<%= WebUtilities.MapUrlPath("/Scripts.aspx") %>'>Manage Scripts</a>]
		</div>
<% } %>
		<asp:placeholder runat="server" id="ScriptHolder"/>
		<div>Starting script...</div>
		<span id="OutputHolder" style="width: 100%; height: 600%;"></span>
		
		
<script language="javascript">

var processID = '<%= Guid.NewGuid() %>';

var timerStarted = false;
var triggerStarted = false;
var isLoading = false;

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Please upgrade to Firefox.");
	}
}

function triggerScript() {

	triggerStarted = true;
	
	var receiveReq = getXmlHttpRequestObject();

	var url = '<%= GetExecuteLink() %>&ProcessID=' + processID;

	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", url, true);
		
 		//receiveReq.onreadystatechange = triggerEvent();
 			
		//Make the actual request.
		receiveReq.send(null);
	}
}

function displayOutput(processID) {

	var receiveReq = getXmlHttpRequestObject();

	var url = '<%= WebUtilities.ConvertApplicationRelativeUrlToAbsoluteUrl("ScriptProcessOutput.aspx") %>?ProcessID=' + processID;

	var output = '';
	
	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		
		receiveReq.open("GET", url, false);
		
		 receiveReq.onreadystatechange = function() {
		  
			output = receiveReq.responseText;
			
			var holder = document.getElementById("OutputHolder");
			
			if (holder)
				holder.innerHTML = output;
				
			scrollDown();
		 }
		 
		//Make the actual request.
		receiveReq.send(null);
		
	}	
	
}

function isComplete(data)
{
	var isComplete = data.indexOf("${ScriptComplete}") > -1;
	
	return isComplete;
}


function timerTick()
{
	var holder = document.getElementById("OutputHolder");
	
	var existingOutput = '';
	
	if (holder)
		existingOutput = holder.innerHTML;
		
	
	if (!isComplete(existingOutput) && !isLoading)
	{
		isLoading = true;
		
		displayOutput(processID);

		isLoading = false;
		
		setTimeout("timerTick()", 2000);

	}
	
}


function startTimer()
{
	setTimeout("timerTick()", 1000);
	
	timerStarted = true;
}

function scrollDown()
{
	var x = 0;  //horizontal coord
	var y = document.height; //vertical coord
	window.scroll(x,y);
}


window.onload = function()
{
	startTimer();
	
	if (!triggerStarted)
		setTimeout("triggerScript()", 10);
}

</script>
</asp:Content>
 


